package com.apes.fn.scm.saleReport.service.process.orderProcess;

import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.fn.crm.customerExpand.FnCustomerExpandRepository;
import com.apes.fn.scm.saleReport.service.process.CreateDetailsProcess;
import com.apes.fn.server.servicePlan.model.ServicePlanItem;
import com.apes.fn.server.servicePlan.repository.ServicePlanItemRepository;
import com.apes.fn.server.workOrder.model.MaintenanceOrder;
import com.apes.fn.server.workOrder.repository.MaintenanceOrderRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.car.model.CarConfiguration;
import com.apes.scm.masterdata.car.model.CarLabel;
import com.apes.scm.masterdata.car.model.CarModel;
import com.apes.scm.masterdata.car.repository.CarConfigurationRepository;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.sal.saleOrder.SaleOrderItem;
import com.apes.scm.sal.saleReturn.SaleReturn;
import com.apes.scm.sal.saleReturn.SaleReturnItem;
import com.apes.scm.sal.saleReturn.SaleReturnRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 分销退货单生成营业额明细
 */
@Service
public class SaleReturnProcess extends CreateDetailsProcess {
    @Autowired
    private SaleReturnRepository saleReturnRepository;
    @Autowired
    private CarConfigurationRepository carConfigurationRepository;
    @Autowired
    private FnCustomerExpandRepository fnCustomerExpandRepository;
    @Autowired
    private ServicePlanItemRepository servicePlanItemRepository;
    @Autowired
    private MaintenanceOrderRepository maintenanceOrderRepository;

    @Override
    public void process(SimpleRequest request) {
        String id = request.get("id");
        SaleReturn saleReturn = saleReturnRepository.findOne(id);
        saleReturn.getSaleReturnItems().forEach(saleReturnItem -> createDetails(MapUtil.mapper("order", saleReturnItem)));
    }

    /**
     * @param map {"order": SaleReturnItem}
     *
     */
    @Override
    public Map<String, Object> getModel(Map map) {
        SaleReturnItem saleReturnItem = (SaleReturnItem) map.get("order");
        SaleReturn saleReturn = saleReturnItem.getSaleReturn();
        Product product = saleReturnItem.getProduct();
        String fnCustomerExpandId = saleReturn.getFnCustomerExpandId();
        //厂牌
        CarLabel carLabel = null;
        //车型
        CarModel carModel = null;
        //车牌
        String carLicense = "";
        if(fnCustomerExpandId != null){
            //车辆扩展信息
            FnCustomerExpand fnCustomerExpand = fnCustomerExpandRepository.findOne(fnCustomerExpandId);
            carLicense = fnCustomerExpand.getCarLicense();
            String jekunModelCode = fnCustomerExpand.getJekunModelCode();
            if(jekunModelCode != null) {
                //配置车型
                CarConfiguration carConfiguration = carConfigurationRepository.findOne(jekunModelCode);
                if(carConfiguration != null){
                    carLabel = carConfiguration.getCarLabel();
                    carModel = carConfiguration.getCarModel();
                }
            }
        }
        ServicePlanItem servicePlanItem = null;
        MaintenanceOrder maintenanceOrder = null;
        String worker = "";
        //通过来源行号找方案明细
        if(saleReturnItem.getSaleOrderItem() != null){
            servicePlanItem = servicePlanItemRepository.findOne(saleReturnItem.getSaleOrderItem().getOriginItemId());
            if(servicePlanItem != null){
                //如果方案明细有关联工单号
                if (servicePlanItem.getRelevanceWorkOrder() != null) {
                    maintenanceOrder = maintenanceOrderRepository.findOne(servicePlanItem.getRelevanceWorkOrder());
                } else {
                    //没有关联工单号就通过方案明细id找工单
                    maintenanceOrder = maintenanceOrderRepository.findByServicePlanItemAndBusinessType(servicePlanItem,"common");
                }
            }
        }
        PartyRole mainSalesman = null;
        PartyRole subSalesman = null;
        if(servicePlanItem != null){
            mainSalesman = servicePlanItem.getMainSaler();
            subSalesman = servicePlanItem.getSupSaler();
        }
        if(maintenanceOrder != null) worker = maintenanceOrder.getWorker1() + "|" +maintenanceOrder.getWorker2() + "|" + maintenanceOrder.getWorker2() + "|" + maintenanceOrder.getWorker2();
        return MapUtil.mapper("carLicense",carLicense,"product",product,
                "productUom",saleReturnItem.getUom(),"qty",saleReturnItem.getProductUomQty(),"salesAmount",saleReturnItem.getPriceTotal() * -1,
                "serveType",product.getServeType(),"dept",saleReturn.getDept(),"company",saleReturn.getCompany(),
                "origin",saleReturn.getId(),"originItem",saleReturnItem.getId(),"originBusinessType","saleReturn",
                "originCreateDate",saleReturn.getDateDone(),"source", saleReturn.getOrigin(),"sourceBusinessType","saleOrder",
                "productGroup",product.getProductGroup(),"classify",product.getClassify(),
                "brand",product.getBrand(),"tax",saleReturnItem.getTax(),"carLabel",carLabel,"carModel",carModel,
                "pftPriceUnit",saleReturnItem.getPriceUnit(),"priceUnit",saleReturnItem.getPriceUnit(),
                "discountAmount",saleReturnItem.getDiscount(),"couponDiscountAmount",0.00,
                "mainSalesman",mainSalesman,"subSalesman",subSalesman,"worker",worker);
    }

    @Override
    public boolean support(String type) {
        return "saleReturn".equals(type);
    }

}
